<?php



namespace App\Http\Controllers\Api\Collection;


use App\Http\Controllers\Api\BasicController;
use App\Models\Coll\Collection;
use App\Models\Coll\CollOrder;
use App\Models\Coll\UserCollection;
use Jiannei\Response\Laravel\Support\Facades\Response;

class CollOrderController extends BasicController
{
    public function place(){
        $price=$this->request->input('price',config('base.burn_price'));
        try {
            $debris = (new CollOrder())->place($this->uid(),$price);
        }catch (\Exception $e){
            return Response::fail($e->getMessage());
        }
        return Response::success($debris);

    }


    public function warehouse(){
        $type=$this->request->input('type',1);
        $result= UserCollection::with('debris')->where(['uid'=>$this->uid(),'type'=>$type])->paginate();
        return Response::success($result);
    }

    public function cast(){
        $ids=$this->request->input('ids',[]);
        $c_id=$this->request->input('c_id');
        $uid=$this->uid();
        $count=UserCollection::where(['uid'=>$uid])->whereIn('id',$ids)->count();
        $coll=Collection::find($c_id);

        if($coll->debris_num == $count){
            UserCollection::create(['uid'=>$uid,'type'=>2,'c_id'=>$coll->id]);
            return Response::success($coll);
        }

        return  Response::fail('铸造失败');
    }
}
